<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
            <el-form-item label="所属部门" prop="depId">
                <el-select v-model="queryParams.depId" size="small" placeholder="请选择：">
                    <el-option
                            v-for="item in departmentOption"
                            :key="item.id"
                            :label="item.depName"
                            :value="item.id">
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="用户名" size="small" prop="userName">
                <el-input
                        v-model="queryParams.userName"
                        placeholder="请输入用户名"
                        clearable
                        style="width: 240px;"
                        size="small"/>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery"
                           v-has="'basic:userinfo:page'"
                >搜索</el-button>
                <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>

        <el-row :gutter="10">
            <el-col :span="1.5">
                <el-button
                        size="small"
                        type="primary"
                        icon="el-icon-plus"
                        @click="getdetails(null)"
                        v-has="'basic:userinfo:add'"
                >新增
                </el-button>
            </el-col>
        </el-row>
        <el-table v-loading="loading" :data="list">
            <el-table-column label="选择" width="50" align="center" :row-class-name="tableRowClassName"
                             @current-change="handleCurrentChange">
                <template slot-scope="scop">
                    <el-radio v-model="radio" :label="scop.$index"><span>&nbsp;</span></el-radio>
                </template>
            </el-table-column>
            <el-table-column label="用户表id" align="center" prop="id" v-if="false"/>
            <el-table-column label="用户姓名" align="center" prop="userName"/>
            <el-table-column label="真实姓名" align="center" prop="realName"/>
            <el-table-column label="部门名称" align="center" prop="depName"/>
            <el-table-column label="职位名称" align="center" prop="positionName"/>
            <el-table-column label="性别"  :formatter="SexFormatter" align="center" prop="sex"/>
            <el-table-column label="电话号码" align="center" prop="phoneNumber"/>
            <el-table-column label="邮箱" align="center" prop="myeMail"/>
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                <template slot-scope="scope">
                    <el-button
                            size="mini"
                            type="text"
                            icon="el-icon-edit"
                            @click="getdetails(scope.row)"
                            v-has="'basic:userinfo:update'"
                    >修改
                    </el-button>
                    <el-button
                            size="mini"
                            type="text"
                            icon="el-icon-edit"
                            @click="resetUser(scope.row)"
                            v-has="'basic:userinfo:resetPassword'"
                    >重置密码
                    </el-button>
                    <el-button
                            size="mini"
                            type="text"
                            icon="el-icon-edit"
                            @click="remove(scope.row)"
                            v-has="'basic:userinfo:delete'"
                    >删除
                    </el-button>
                </template>
            </el-table-column>
        </el-table>

        <pagination
                v-show="total>0"
                :total="total"
                :page.sync="queryParams.pageNum"
                :limit.sync="queryParams.pageSize"
                @pagination="getList"
        />
        <!-- 添加或修改参数配置对话框 -->
        <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
            <el-form :model="form" :rules="rules" ref="loginForm" class="login-form" label-width="98px">
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="账户名：" prop="userName">
                            <el-input v-model="form.userName"/>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="性别：" prop="sex">
                            <el-radio-group v-model="form.sex">
                                <el-radio v-for="datastate in SexOptions"
                                          :key="datastate.dataType"
                                          :label="datastate.dataType">
                                    {{datastate.dataName}}
                                </el-radio>
                            </el-radio-group>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="昵称：" prop="realName">
                            <el-input v-model="form.realName"/>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="角色：" prop="roleIds">
                            <el-select v-model="form.roleIds" multiple placeholder="请选择：">
                                <el-option
                                        v-for="item in rolesOption"
                                        :key="item.id"
                                        :label="item.roleName"
                                        :value="item.id">
                                </el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="职位：" prop="positionName">
                            <el-input v-model="form.positionName"/>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="所属部门：" prop="depIds">
                            <el-select v-model="form.depIds" multiple placeholder="请选择：">
                                <el-option
                                        v-for="item in departmentOption"
                                        :key="item.id"
                                        :label="item.depName"
                                        :value="item.id">
                                </el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="微信号：" prop="wechatNumber">
                            <el-input v-model="form.wechatNumber"/>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="联系电话：" prop="phoneNumber">
                            <el-input v-model="form.phoneNumber"/>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="qq号：" prop="qqNumber">
                            <el-input v-model="form.qqNumber"/>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="联系地址：" prop="address">
                            <el-input v-model="form.address"/>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="邮箱：" prop="myeMail">
                            <el-input v-model="form.myeMail"/>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="办公电话：" prop="officeTelephone">
                            <el-input v-model="form.officeTelephone"/>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="24">
                        <el-form-item label="备注" prop="remark">
                            <el-input type="textarea" v-model="form.remark"/>
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitForm">确 定</el-button>
                <el-button @click="close">取 消</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
    import {delUser,userlist, getUser, addUser, updateUser,resetPassword} from "@/api/user/user";
    import {rolelist} from "@/api/user/roles";
    import {departmentlist} from "@/api/user/department";

    export default {
        name: "userInfo",
        data() {
            return {
                //单选框
                radio: '',
                //码值
                SexOptions: [],
                departmentOption: [],
                rules: {
                    userName: [
                        {required: true, message: '请输入用户名', trigger: 'blur'},
                        {max: 32, message: '长度在 32 个字符', trigger: 'blur'}
                    ],
                    phoneNumber: [
                        {required: true, pattern: /^1[34578]\d{9}$/, message: '请输入正确的手机号（中国手机号）', trigger: 'blur'}
                    ],
                    myeMail: [
                        {required: true, pattern: /^([a-zA-Z]|[0-9])(\w|-)+@[a-zA-Z0-9]+\.([a-zA-Z]{2,4})$/, message: '请输入正确的邮箱号', trigger: 'blur'}
                    ],
                    depIds:[
                        {required: true, message: '请选择所属部门', trigger: 'blur'},

                    ],
                    roleIds:[
                        {required: true, message: '请分配角色', trigger: 'blur'},

                         ]
                },
                title: '查看详情',
                open: false,
                // 遮罩层
                loading: false,
                // 选中数组
                ids: [],
                // 非多个禁用
                multiple: true,
                // 显示搜索条件
                showSearch: true,
                rolesOption: [],
                // 总条数
                total: 0,
                // 表格数据
                list: [],
                // 表单参数
                form: {
                    positionName: null,
                    roleIds: [],
                    depIds: [],
                },
                // 日期范围
                dateRange: [],
                // 查询参数
                queryParams: {
                    pageNum: 1,
                    pageSize: 10,
                    userName:null,
                    depId:null
                }
            };
        },
        created() {
            this.getList();
            this.getDicdataByType("sex_").then(response => {
                this.SexOptions = response.data;
            });
            this.getRolesList();
            this.getDepartmentlist();
        },
        methods: {
            SexFormatter(row) {
                return this.selectLabel(this.SexOptions, row.state);
            },
            /** 查询部门下拉框列表 */
            getDepartmentlist() {
                departmentlist().then(response => {
                        this.departmentOption = response.data;
                    }
                );
            },
            /** 查询角色下拉框列表 */
            getRolesList() {
                rolelist().then(response => {
                        this.rolesOption = response.data;
                    }
                );
            },
            /** 查询登录日志列表 */
            getList() {
                this.loading = true;
                userlist(this.queryParams).then(response => {
                        this.list = response.data.records;
                        this.total = response.data.total;
                        this.loading = false;
                    }
                );
            },
            getdetails(row) {
                this.reset();
                if (row != null) {
                    getUser(row.id).then(response => {
                        this.form = response.data;
                    });
                    this.title = '修改员工信息';
                }else{
                    this.title = '新增员工信息';
                }
                this.open = true;
            },
            /** 搜索按钮操作 */
            handleQuery() {
                this.getList();
            },
            /** 重置按钮操作 */
            resetQuery() {
                this.resetForm("queryForm");
                this.queryParams.pageNum = 1;
            },
            submitForm() {
                this.$refs["loginForm"].validate(valid => {
                    if (valid) {
                        if (this.form.id == null || this.form.id == undefined) {
                            addUser(this.form).then(response => {
                                if (response.code == 0) {
                                    this.msgSuccess("新增成功成功");
                                    this.open = false;
                                    this.handleQuery();
                                } else {
                                    this.msgError(response.msg);
                                }
                            })
                        } else {
                            updateUser(this.form).then(response => {
                                if (response.code == 0) {
                                    this.msgSuccess("修改成功");
                                    this.open = false;
                                    this.handleQuery();
                                } else {
                                    this.msgError(response.msg);
                                }
                            })
                        }
                    }
                });
            },
            close() {
                this.title = '';
                this.open = false;
                this.reset();
            },
            reset() {
                // 表单参数
                this.form = {
                    userName: null,
                    sex: '1',
                    realName:null,
                    depId: null,
                    positionId: null,
                    positionName: null,
                    wechatNumber: null,
                    phoneNumber: null,
                    qqNumber: null,
                    address: null,
                    myeMail: null,
                    officeTelephone: null,
                    remark: null,
                    roleIds: [],
                };
                this.resetForm("loginForm");
            },
            resetUser(row){
                resetPassword(row.userName).then(response => {
                    if (response.code == 0) {
                        this.msgSuccess("重置成功");
                    } else {
                        this.msgError(response.msg);
                    }
                    this.handleQuery();
                });
            },
            remove(row){
                delUser(row.id).then(response => {
                    if (response.code == 0) {
                        this.msgSuccess("删除成功");
                    } else {
                        this.msgError(response.msg);
                    }
                    this.handleQuery();
                });
            },
            tableRowClassName({row, rowIndex}) {
                row.index = rowIndex;
            },
            handleCurrentChange(row) {
                if (row != null) {
                    this.radio = row.index
                }
            },
        }
    };
</script>
<style>
</style>
